package net.oschina.app.fragment.me.favorite;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;

import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;

import net.oschina.app.adapter.MeFavoriteRvAdapter;
import net.oschina.app.base.BaseAdapter;
import net.oschina.app.base.BaseFragment;
import net.oschina.app.bean.CollectBean;
import net.oschina.app.protocol.me_favorite.MeFavoriteProtocol;
import net.oschina.app.ui.DetailActivity;
import net.oschina.app.util.CommonUtils;

import java.util.List;

/**
 * Created by wei03 on 2017/1/14.
 */
public class TopicFragment extends BaseFragment<CollectBean> {
    private MeFavoriteRvAdapter mTopicRvAdapter;
    private List<CollectBean.FavoriteBean> mFavorites;


    @Override
    protected void OnSubItemClick(RecyclerAdapterWithHF adapter, RecyclerView.ViewHolder vh, int position) {
        super.OnSubItemClick(adapter, vh, position);
        Intent intent = new Intent(getContext(), DetailActivity.class);
        CollectBean.FavoriteBean favoriteBean = mFavorites.get(position);
        intent.putExtra("url", favoriteBean.getUrl());
        intent.putExtra("title", getFragmentTitle());
        startActivity(intent);
    }

    @Override
    protected BaseAdapter getRvAdapter() {
        mTopicRvAdapter = new MeFavoriteRvAdapter();
        return mTopicRvAdapter;
    }

    @Override
    protected int onLoadSubData() {
        Bundle arguments = getArguments();
        String type = "";
        if (arguments != null) {
            type = arguments.getString("type");
        }
        MeFavoriteProtocol softwaeProtocol = new MeFavoriteProtocol(type);
        final CollectBean topic = softwaeProtocol.getData(mList.size());
        if (topic != null) {
            CommonUtils.runOnMainThread(new Runnable() {
                @Override
                public void run() {
                    mFavorites = topic.getFavorites();
                    mTopicRvAdapter.setDatas(mFavorites);
                }
            });
            return STATE_SUCCESS;
        }
        return STATE_EMPTY;
    }
}
